home *** CD-ROM | disk | FTP | other *** search
- import java.io.BufferedWriter;
- import java.io.FileInputStream;
- import java.io.FileWriter;
- import java.io.Writer;
- import java.util.Properties;
- import java.util.StringTokenizer;
- import java.util.Vector;
-
- public class Engine {
- protected ChannelList database = new ChannelList();
- protected Config theConfig = new Config();
- protected Vector keywords = new Vector(100, 10);
-
- public Engine(Config systemConfig) {
- this.theConfig = systemConfig;
- }
-
- protected void updateHeadlines() throws Exception {
- for(int i = 0; i < this.database.size(); ++i) {
- Channel tempChannel = this.database.getChannel(i);
- Parser theParser = new Parser(tempChannel);
-
- try {
- tempChannel.setHeadlines(theParser.findHeadlines());
- tempChannel.setDownloaded(true);
- this.database.setChannel(i, tempChannel);
- } catch (Exception var6) {
- String[] temp = new String[]{"NewsMac Error", ((Throwable)var6).toString().substring(21, ((Throwable)var6).toString().length())};
- tempChannel.setHeadlines(temp);
- tempChannel.setDownloaded(false);
- this.database.setChannel(i, tempChannel);
- }
- }
-
- }
-
- protected void updateHeadlines(Channel theChannel) throws Exception {
- Parser theParser = new Parser(theChannel);
-
- try {
- theChannel.setHeadlines(theParser.findHeadlines());
- this.database.setChannel(this.getChannelIndex(theChannel), theChannel);
- theChannel.setDownloaded(true);
- } catch (Exception var5) {
- String[] temp = new String[]{"NewsMac Error", ((Throwable)var5).toString().substring(21, ((Throwable)var5).toString().length())};
- theChannel.setHeadlines(temp);
- theChannel.setDownloaded(false);
- }
-
- }
-
- protected String[] getChannelFiles(String path) throws Exception {
- try {
- String[] temp = IOUtil.dirListing(path);
- return temp;
- } catch (Exception var4) {
- System.out.println(var4);
- throw new Exception("Error locating channel files!");
- }
- }
-
- private int getChannelIndex(Channel theChannel) throws Exception {
- for(int i = 0; i < this.database.size(); ++i) {
- if (this.database.getChannel(i).getName().equals(theChannel.getName())) {
- return i;
- }
- }
-
- throw new Exception("Channel not in database!");
- }
-
- protected ChannelList getChannels() {
- return this.database;
- }
-
- protected ChannelList getCategory(int category) {
- ChannelList tempList = new ChannelList();
-
- for(int i = 0; i < this.database.size(); ++i) {
- Channel tempChannel = this.database.getChannel(i);
- if (tempChannel.getCategory() == category) {
- tempList.addChannel(tempChannel);
- }
- }
-
- return tempList;
- }
-
- protected String getKeywords() {
- String temp = "";
-
- for(int i = 0; i < this.keywords.size(); ++i) {
- if (i + 1 == this.keywords.size()) {
- temp = temp + (String)this.keywords.elementAt(i);
- } else {
- temp = temp + (String)this.keywords.elementAt(i) + ", ";
- }
- }
-
- return temp;
- }
-
- protected ChannelList getFavorites() {
- ChannelList tempList = new ChannelList();
-
- for(int i = 0; i < this.database.size(); ++i) {
- Channel tempChannel = this.database.getChannel(i);
- if (tempChannel.isFavorite()) {
- tempList.addChannel(tempChannel);
- }
- }
-
- return tempList;
- }
-
- protected boolean loadKeywords() {
- this.keywords = new Vector(100, 10);
-
- Vector temp;
- try {
- temp = IOUtil.openTextFile(this.theConfig.getKeywordFile());
- } catch (Exception var6) {
- System.out.println("Engine: Could not load keywords file, using defaults.\n\t" + var6);
- this.keywords.add("Mac");
- this.keywords.add("Apple");
- this.keywords.add("NewsMac");
- this.keywords.add("OS X");
- return false;
- }
-
- String tempString = "";
-
- for(int i = 0; i < temp.size(); ++i) {
- tempString = tempString + (String)temp.elementAt(0);
- }
-
- StringTokenizer st = new StringTokenizer(tempString, ",");
- int n = 0;
-
- while(st.hasMoreTokens()) {
- this.keywords.add(st.nextToken().trim());
- }
-
- return true;
- }
-
- protected Channel openChannel(String fileName) throws Exception {
- Channel temp = new Channel();
-
- try {
- Properties props = new Properties();
- props.load(new FileInputStream(fileName));
- temp.setName(props.getProperty("name"));
- temp.setURL(props.getProperty("url"));
- temp.setSource(props.getProperty("source"));
- temp.setDescription(props.getProperty("description"));
- temp.setHeadlineCount(new Integer(props.getProperty("capture")));
- temp.setCategory(new Integer(props.getProperty("category")));
- temp.setVersion(new Integer(props.getProperty("version")));
- if (props.getProperty("cache").equalsIgnoreCase("yes")) {
- temp.setCached(true);
- } else {
- temp.setCached(false);
- }
-
- temp.setStartMarker(props.getProperty("start_marker"));
- temp.setEndMarker(props.getProperty("end_marker"));
- temp.setOpeningMarker(props.getProperty("headline_start"));
- temp.setClosingMarker(props.getProperty("headline_end"));
- return temp;
- } catch (Exception var4) {
- throw new Exception("Error reading channel " + fileName + "\n");
- }
- }
-
- protected boolean loadAllChannels(boolean system) {
- String[] dirListing = new String[]{""};
- boolean noError = true;
-
- try {
- if (system) {
- this.database = new ChannelList();
- Vector channelDatabase = IOUtil.openTextFile("newsmac.app/contents/resources/channel_database.data");
-
- for(int i = 0; i < channelDatabase.size(); ++i) {
- Channel temp = new Channel();
- StringTokenizer st = new StringTokenizer((String)channelDatabase.elementAt(i), "~~");
-
- while(st.hasMoreTokens()) {
- try {
- if (i <= 0) {
- this.database.setVersion(new Integer(st.nextToken()));
- } else {
- temp.setName(st.nextToken());
- temp.setURL(st.nextToken());
- temp.setSource(st.nextToken());
- temp.setDescription(st.nextToken());
- temp.setStartMarker(st.nextToken());
- temp.setEndMarker(st.nextToken());
- temp.setOpeningMarker(st.nextToken());
- temp.setClosingMarker(st.nextToken());
- temp.setVersion(new Integer(st.nextToken()));
- temp.setCategory(new Integer(st.nextToken()));
- temp.setHeadlineCount(new Integer(st.nextToken()));
- temp.setMaxHeadlineCount(new Integer(st.nextToken()));
- if (temp.getStartMarker().equals(" ")) {
- temp.setStartMarker("");
- }
-
- if (temp.getEndMarker().equals(" ")) {
- temp.setEndMarker("");
- }
-
- int channelCounter = 0;
- if (this.database.size() <= 5) {
- this.database.addChannel(temp);
- } else {
- for(int z = 0; z < this.database.size(); ++z) {
- if (this.database.getChannel(z).getName().equals(temp.getName())) {
- ++channelCounter;
- }
- }
-
- if (channelCounter < 2) {
- this.database.addChannel(temp);
- }
- }
- }
- } catch (Exception var11) {
- System.out.println("Engine: Error loading channel file from database\n\t" + var11);
- }
- }
- }
- } else if (IOUtil.fileExists(this.theConfig.getHome() + "/library/channels/")) {
- dirListing = this.getChannelFiles(this.theConfig.getHome() + "/library/channels/");
-
- for(int i = 0; i < dirListing.length; ++i) {
- try {
- if (dirListing[i].endsWith(".channel")) {
- this.database.addChannel(this.openChannel(this.theConfig.getHome() + "/library/channels/" + dirListing[i]));
- }
- } catch (Exception var10) {
- System.out.println("Engine: Could not load channel " + dirListing[i] + "!\n\t" + var10);
- noError = false;
- }
- }
- }
-
- if (dirListing.length == 0) {
- throw new Exception("No channels were found!");
- } else {
- return noError;
- }
- } catch (Exception var12) {
- System.out.println("Engine: Could not load channels!\n\t" + var12);
- return false;
- }
- }
-
- protected void saveChannelSettings() throws Exception {
- try {
- BufferedWriter out = new BufferedWriter(new FileWriter(this.theConfig.getHome() + "/library/preferences/NewsMac Channel Settings", false));
-
- for(int i = 0; i < this.database.size(); ++i) {
- Channel tempChannel = this.database.getChannel(i);
- String temp = tempChannel.getName() + "~~" + tempChannel.isFavorite() + "~~" + tempChannel.isOn() + "~~" + tempChannel.isEnabled() + "\n";
- ((Writer)out).write(temp);
- }
-
- out.close();
- } catch (Exception var5) {
- throw new Exception("Unable to save channel settings file!");
- }
- }
-
- protected void applyChannelSettings() throws Exception {
- try {
- if (IOUtil.fileExists(this.theConfig.getHome() + "/library/preferences/NewsMac Channel Settings")) {
- Vector channelStates = IOUtil.openTextFile(this.theConfig.getHome() + "/library/preferences/NewsMac Channel Settings");
- String channelName = "";
- String channelFavorite = "";
- String channelOn = "";
- String channelEnabled = "";
-
- for(int i = 0; i < channelStates.size(); ++i) {
- StringTokenizer st = new StringTokenizer((String)channelStates.elementAt(i), "~~");
-
- try {
- channelName = st.nextToken().trim();
- channelFavorite = st.nextToken().trim();
- channelOn = st.nextToken().trim();
- channelEnabled = st.nextToken().trim();
- } catch (Exception var10) {
- System.out.println("Error processing settigns for line " + i);
- }
-
- for(int j = 0; j < this.database.size(); ++j) {
- Channel temp = this.database.getChannel(j);
- if (temp.getName().equals(channelName)) {
- temp.setFavorite(false);
- temp.setEnabled(false);
- temp.setOn(false);
- if (channelFavorite.equals("true")) {
- temp.setFavorite(true);
- }
-
- if (channelOn.equals("true")) {
- temp.setOn(true);
- }
-
- if (channelEnabled.equals("true") & temp.isFavorite() || channelEnabled.equals("true") & this.theConfig.isSaveAll()) {
- temp.setEnabled(true);
- }
-
- this.database.setChannel(j, temp);
- }
- }
- }
- }
-
- } catch (Exception var11) {
- System.out.println(var11);
- throw new Exception("Channel settings file could not be read!\n" + var11);
- }
- }
-
- protected Vector recommendedList() throws Exception {
- Vector tempList = new Vector(100, 50);
- if (this.keywords.size() > 0) {
- for(int i = 0; i < this.database.size(); ++i) {
- Channel tempChannel = this.database.getChannel(i);
- String[] headlines = tempChannel.getHeadlines();
-
- for(int j = 0; j < headlines.length; ++j) {
- int hits = 0;
-
- for(int k = 0; k < this.keywords.size(); ++k) {
- try {
- if (headlines[j] != null && ((String)this.keywords.elementAt(k)).length() <= headlines[j].length() && StringUtil.contains(headlines[j], (String)this.keywords.elementAt(k))) {
- ++hits;
- }
- } catch (Exception var9) {
- throw new Exception("Error while creating personalised news.");
- }
- }
-
- if (hits > 0) {
- tempList.add(new Headline(tempChannel.getName(), tempChannel.getURL(), headlines[j], hits, tempChannel.getCategory()));
- }
- }
- }
-
- for(int c = 0; c < tempList.size(); ++c) {
- for(int i = 0; i < tempList.size() - 1; ++i) {
- if (((Headline)tempList.elementAt(i)).getKeywordMatch() < ((Headline)tempList.elementAt(i + 1)).getKeywordMatch()) {
- Headline tempHeadline = (Headline)tempList.elementAt(i);
- tempList.setElementAt(tempList.elementAt(i + 1), i);
- tempList.setElementAt(tempHeadline, i + 1);
- }
- }
- }
- }
-
- return tempList;
- }
-
- protected Vector searchHeadlines(String searchString) throws Exception {
- Vector tempList = new Vector(100, 50);
- Vector searchWords = new Vector(10, 5);
- StringTokenizer st = new StringTokenizer(searchString, " ");
-
- while(st.hasMoreTokens()) {
- searchWords.add(st.nextToken().trim());
- }
-
- if (searchWords.size() > 0) {
- for(int i = 0; i < this.database.size(); ++i) {
- Channel tempChannel = this.database.getChannel(i);
- String[] headlines = tempChannel.getHeadlines();
-
- for(int j = 0; j < headlines.length; ++j) {
- int hits = 0;
-
- for(int k = 0; k < searchWords.size(); ++k) {
- try {
- if (headlines[j] != null && ((String)searchWords.elementAt(k)).length() <= headlines[j].length() && StringUtil.contains(headlines[j], (String)searchWords.elementAt(k))) {
- ++hits;
- }
- } catch (Exception var12) {
- throw new Exception("Error while searching headlines!");
- }
- }
-
- if (hits > 0) {
- tempList.add(new Headline(tempChannel.getName(), tempChannel.getURL(), headlines[j], hits, tempChannel.getCategory()));
- }
- }
- }
-
- for(int c = 0; c < tempList.size(); ++c) {
- for(int i = 0; i < tempList.size() - 1; ++i) {
- if (((Headline)tempList.elementAt(i)).getKeywordMatch() < ((Headline)tempList.elementAt(i + 1)).getKeywordMatch()) {
- Headline tempHeadline = (Headline)tempList.elementAt(i);
- tempList.setElementAt(tempList.elementAt(i + 1), i);
- tempList.setElementAt(tempHeadline, i + 1);
- }
- }
- }
- }
-
- return tempList;
- }
-
- protected void startupActions(NewsMac parent) {
- if (this.theConfig.isAutoDownload()) {
- if (this.theConfig.isDownloadFavorites()) {
- this.downloadChannels(parent, this.getFavorites(), this.theConfig.isMyNewsSwitch());
- } else if (this.theConfig.isDownloadAdd()) {
- this.downloadChannels(parent, this.database, this.theConfig.isMyNewsSwitch());
- }
- }
-
- }
-
- protected void downloadChannels(NewsMac parent, ChannelList channelsToDownload, boolean autoSwitch) {
- try {
- if (!this.theConfig.isMyNewsSwitch()) {
- autoSwitch = false;
- }
-
- if (!this.theConfig.isBroadband()) {
- Thread queue = new QueuedDownload(parent, channelsToDownload, autoSwitch);
- queue.start();
- } else {
- for(int i = 0; i < channelsToDownload.size(); ++i) {
- Channel tempChannel = channelsToDownload.getChannel(i);
- tempChannel.setDownloaded(false);
- String[] temp = new String[]{"Updating..."};
- tempChannel.setHeadlines(temp);
- Thread theDownloader = new Downloader(tempChannel, parent, autoSwitch);
- theDownloader.start();
- }
-
- parent.updatePanel(parent.currentPanel);
- }
- } catch (Exception var8) {
- System.out.println("Engine: " + var8);
- }
-
- }
- }
-